#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <string.h>

/** Autuor : zhaoshun
  * Time   : 2017/12/20 17:58
  * Email  : tmby1314@163.com
  **/
int main_socket_client(int argc,char **argv) {

    if (NULL == argv[1] || NULL == argv[2] || NULL == argv[3]) {
        printf("Usage : client ip port file\n");
        exit(10);
    }

    int sockfd,fd,n;
    struct hostent *he;
    struct sockaddr_in their_addr;
    char buf[1024];
    he=gethostbyname(argv[1]);    /*socket()*/
    sockfd=socket(AF_INET,SOCK_STREAM,0);
    their_addr.sin_family = AF_INET;
    their_addr.sin_port = htons(atoi(argv[2]));
    their_addr.sin_addr = *((struct in_addr *)he->h_addr);
    bzero(&(their_addr.sin_zero),8);    /*connect()*/
    connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr));
    fd=open(argv[3],O_RDONLY,0644);  /*send()*/
    while((n=read(fd,buf,1024))>0)  {
        printf("%d\n",n);
        if(n<1024)
            buf[n]='\0';
        send(sockfd,buf,n,0);   }
    close(fd);
    close(sockfd);
    return 0;
}